package com.wtwd.campus.hxy.model.vo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wtwd.campus.hxy.constant.RespCode;
import com.wtwd.campus.model.vo.PageDataVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zdl
 * @Description
 * @create 2021-06-15 13:35
 */
@ApiModel(value="接口返回对象", description="接口返回对象")
public class ResultVO<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 返回消息
     */
    @ApiModelProperty(value = "返回消息")
    private String msg = "操作成功！";

    /**
     * 返回代码
     */
    @ApiModelProperty(value = "返回代码")
    private Integer code = 1;

    /**
     * 返回数据对象 data
     */
    @ApiModelProperty(value = "返回数据对象")
    private T data;


    public ResultVO() {

    }

    public ResultVO<T> ok(String message) {
        this.msg = message;
        this.code = RespCode.SUCCESS.getCode();
        return this;
    }

    public static<T> ResultVO<PageDataVO<T>> ok(IPage pageData) {
        ResultVO<PageDataVO<T>> r = new ResultVO<>();
        r.setCode(RespCode.SUCCESS.getCode());
        r.setMsg(RespCode.SUCCESS.getMsg());
        if (pageData == null) {
            return r;
        }
        PageDataVO<T> data = new PageDataVO<>();
        data.setTotal(pageData.getTotal());
        data.setRows(pageData.getRecords());
        r.setData(data);
        return r;
    }



    public static<T> ResultVO<T> ok() {
        ResultVO<T> r = new ResultVO<T>();
        r.setCode(RespCode.SUCCESS.getCode());
        r.setMsg(RespCode.SUCCESS.getMsg());
        return r;
    }

    public static<T> ResultVO<T> ok(T data) {
        ResultVO<T> r = new ResultVO<>();
        r.setCode(RespCode.SUCCESS.getCode());
        r.setData(data);
        return r;
    }

    public static<T> ResultVO<T> ok(String msg, T data) {
        ResultVO<T> r = new ResultVO<T>();
        r.setCode(RespCode.SUCCESS.getCode());
        r.setMsg(msg);
        r.setData(data);
        return r;
    }


    public static ResultVO<Object> error(int code, String msg) {
        ResultVO<Object> r = new ResultVO<Object>();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

    public static<T> ResultVO<T> error(RespCode respCode) {
        ResultVO<T> r = new ResultVO<>();
        r.setCode(respCode.getCode());
        r.setMsg(respCode.getMsg());
        return r;
    }

    public static Object okWithOutData(){
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("message","操作成功");
        return map;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
